﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
using System.Drawing;

namespace Portal.Modules.ImageBrowser
{
  /// <summary>
  /// The Configuration of the image Browser instance.
  /// </summary>
  public class ImageBrowserConfig
  {
    [XmlIgnore]
    private string _pictVirtualPath = "~/Data/Resources/Pictures/";

    /// <summary>
    /// The virtual relativ directory of the images ie. /pics
    /// </summary>
    [XmlElement("PictureVirtualDirectory")]
    public string PictureVirtualPath
    {
      get
      {
        return _pictVirtualPath;
      }
      set
      {
        _pictVirtualPath = VirtualPathUtility.AppendTrailingSlash(value);
        _picturePhysicalPath = HttpContext.Current.Server.MapPath(_pictVirtualPath);
      }
    }

    /// <summary>
    /// The absolute physical path of the images.
    /// </summary>
    private string _picturePhysicalPath;

    /// <summary>
    /// The absolute physical path of the images.
    /// </summary>
    [XmlIgnore]
    public string PicturePhysicalPath
    {
      get
      {
        if(_picturePhysicalPath == null)
          _picturePhysicalPath = HttpContext.Current.Server.MapPath(_pictVirtualPath);
        return _picturePhysicalPath;
      }
    }
    

    [XmlElement("ThumbnailCount")]
    public int ThumbnailCount = 0;

    [XmlElement("ThumbnailAreaWidth")]
    public int ThumbnailAreaWidth = 140;

    [XmlElement("ThumbnailAreaHeight")]
    public int ThumbnailAreaHeight = 150;

    public class ImageCfg
    {
      public ImageCfg()
      {
      }

      public ImageCfg(int maxSize, int shadowWidth)
      {
        MaxSize = maxSize;
        ShadowWidth = shadowWidth;
      }
      [XmlElement("JpegQuality")]
      public byte JpegQuality = 90;

      [XmlElement("MaxSize")]
      public int MaxSize = 100;

      [XmlElement("Shadow")]
      public bool HasShadow = true;

      [XmlElement("ShadowWidth")]
      public int ShadowWidth = 8;

      [XmlElement("BorderWidth")]
      public int BorderWidth = 0;

      [XmlElement("ShadowTransparency")]
      public double ShadowTransparency = 0;

      [XmlElement("ShadowColor")]
      public int _shadowColor = Color.FromArgb(0, 0, 0).ToArgb();
      [XmlIgnore]
      public Color ShadowColor { get { return Color.FromArgb(_shadowColor); } set { _shadowColor = value.ToArgb(); } }

      [XmlElement("BackgroundColor")]
      public int _backgroundColor = Color.FromArgb(240, 240, 240).ToArgb();
      [XmlIgnore]
      public Color BackgroundColor { get { return Color.FromArgb(_backgroundColor); } set { _backgroundColor = value.ToArgb(); } }

      [XmlElement("BorderColor")]
      public int _borderColor = Color.FromArgb(0, 0, 0).ToArgb();
      [XmlIgnore]
      public Color BorderColor { get { return Color.FromArgb(_borderColor); } set { _borderColor = value.ToArgb(); } }

      [XmlElement("SoftShadow")]
      public bool SoftShadow = true;

      public override bool Equals(object obj)
      {
        if (obj == null || GetType() != obj.GetType()) 
          return false;

        ImageCfg cfg = (ImageCfg) obj;

        return cfg.JpegQuality == JpegQuality
          && cfg.MaxSize == MaxSize
          && cfg.HasShadow == HasShadow
          && cfg.ShadowWidth == ShadowWidth
          && cfg.BorderWidth == BorderWidth
          && cfg.ShadowTransparency == ShadowTransparency
          && cfg._shadowColor == _shadowColor
          && cfg._backgroundColor == _backgroundColor
          && cfg._borderColor == _borderColor
          && cfg.SoftShadow == SoftShadow;
      }
    }

    [XmlElement("ThumbnailCfg")]
    public ImageCfg ThumbCfg = new ImageCfg(120, 8);

    [XmlElement("PreviewCfg")]
    public ImageCfg PreviewCfg = new ImageCfg(600, 25);

    /// <summary>
    /// A Helper to prevent the image caching. Increment this counter each time the configuration is changed.
    /// </summary>
    [XmlElement("ConfigVersion")]
    public int ConfigVersion = 0;
   
  }
}